<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-col :span="12">
        <el-form-item label="多云账号" prop="dyzh">
          <el-input v-model="dataForm.dyzh" :disabled="disabled" :maxlength="30" placeholder="多云账号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="姓名" prop="zhxm">
          <el-input v-model="dataForm.zhxm" :disabled="disabled" :maxlength="30" placeholder="姓名"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="所属单位" prop="orgNo">
          <org-select v-model="dataForm.orgNo" :disabled="disabled" allowTrunk placeholder="请选择所属单位"
                      @change="orgChange"></org-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="身份证号" prop="zjhm">
          <el-input v-model="dataForm.zjhm" :disabled="disabled" :maxlength="20" placeholder="身份证号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号" prop="sjhm">
          <el-input v-model="dataForm.sjhm" :disabled="disabled" :maxlength="15" placeholder="手机号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="是否离项" prop="isLx">
          <el-select v-model="dataForm.isLx"
                     placeholder="是否离项"
                     :disabled="disabled"
                     class="width100"
                     size="small">
            <el-option
              :key="true"
              label="是"
              :value="true">
            </el-option>
            <el-option
              :key="false"
              label="否"
              :value="false">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="岗位名称" prop="gwmc">
          <el-select v-model="selectDygwList" :disabled="disabled" multiple collapse-tags clearable filterable placeholder="请选择岗位名称"
                     class="width100">
            <el-option
              v-for="item in dygwList"
              :key="item.key"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import OrgSelect from '@/components/org-select'

export default {
  data() {
    return {
      disabled: false,
      visible: false,
      dygwList: [],
      selectDygwList: [],
      dataForm: {
        id: '',
        dyzh: '',
        zhxm: '',
        orgNo: '',
        zjhm: '',
        sjhm: '',
        gwmc: '',
        isLx: false,
        isDeleted: '',
        createTime: '',
        creator: '',
        updateTime: '',
        updater: ''
      },
      dataRule: {
        dyzh: [
          {required: true, message: '多云账号不能为空', trigger: 'blur'}
        ],
        zhxm: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        orgNo: [
          {required: true, message: '所属单位不能为空', trigger: 'blur'}
        ],
        zjhm: [
          {required: true, message: '身份证号不能为空', trigger: 'blur'}
        ],
        sjhm: [
          {required: true, message: '手机号不能为空', trigger: 'blur'}
        ],
        gwmc: [
          {required: true, message: '岗位名称不能为空', trigger: 'blur'}
        ],
        isLx: [
          {required: true, message: '是否离项不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  components: {
    OrgSelect
  },
  methods: {
    init(id, disabled) {
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.selectDygwList = []
        if (this.dataForm.id) {
          this.$http({
            url: `/zr/dyzh/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.dyzh
              let gwmcs = data.dyzh.gwmc
              if (gwmcs) {
                this.selectDygwList = gwmcs.split(',')
              }
            }
          })
        }

        // 初始化岗位选项
        // 远程请求回来的数据
        this.$http('/sys/dict/queryByCode?code=DYGW_LIST')
          .then((response) => {
            for (let i = 0; i < response.data.list.length; i++) {
              this.dygwList[i] = {}
              this.dygwList[i].label = response.data.list[i]['name']
              this.dygwList[i].value = response.data.list[i]['value']
              this.dygwList[i].key = i
            }
            this.$forceUpdate()
          })
      })
    },
    // 表单提交
    dataFormSubmit() {
      if (!this.selectDygwList || this.selectDygwList.length === 0) {
        this.$message.error('请输入岗位')
        return
      }
      this.dataForm.gwmc = this.selectDygwList.join(',')
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/zr/dyzh/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    },
    orgChange(val) {
      this.dataForm.orgNo = val
    }
  }
}
</script>
